//
//  AuthenViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/7.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "AuthenViewController.h"
#import "AuthenTableViewCell.h"
#import "MyTableHeaderView.h"
#import "AuthenView.h"
#import "AuthenView2ViewController.h"
@interface AuthenViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSMutableArray *titleValueArray;
@property(nonatomic,strong)NSMutableArray *attTitleValueArray;
@property(nonatomic,strong)UIButton *nextStypeBtn;
@property(nonatomic,strong)AuthenView *av;



/**
 真实姓名
 */
@property(nonatomic,strong)UITextField *nameTf;

/**
 手机号
 */
@property(nonatomic,strong)UITextField *phoneTf;

/**
 身份证号
 */
@property(nonatomic,strong)UITextField *IDNumTf;

@end

@implementation AuthenViewController

-(NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}
-(NSMutableArray *)titleValueArray
{
    if (_titleValueArray == nil) {
        _titleValueArray = [NSMutableArray array];
    }
    return _titleValueArray;
}
-(NSMutableArray *)attTitleValueArray
{
    if (_attTitleValueArray == nil) {
        _attTitleValueArray = [NSMutableArray array];
    }
    return _attTitleValueArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"实名认证";
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1];
    //创建tableView(实名认证第一步)
    [self createTableView];
    //创建顶部基本信息展示View
    [self createTopView];
    //下一步
    [self createNextStype];
}


-(void)createNextStype
{
    self.nextStypeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.nextStypeBtn.backgroundColor = [UIColor colorWithRed:91 / 255.0 green:113 / 255.0 blue:255 / 255.9 alpha:1];
    [self.nextStypeBtn setTitle:@"下一步" forState:(UIControlStateNormal)];
    [self.nextStypeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.nextStypeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    NSLog(@"%f",ETTopHeight);
    self.nextStypeBtn.frame = CGRectMake(0, self.view.frame.size.height - ETTopHeight - ETTabBarArcHeight - 44, self.view.frame.size.width, 44);
    [self.view addSubview:self.nextStypeBtn];
    [self.nextStypeBtn addTarget:self action:@selector(nextStypeBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)nextStypeBtnAction
{
    NSLog(@"点击下一步");
    if (self.nameTf.text.length == 0) {
        [self showFailedResultTips:@"请输入姓名" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    if (self.phoneTf.text.length == 0) {
        [self showFailedResultTips:@"请输入手机号" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    if (self.IDNumTf.text.length == 0) {
        [self showFailedResultTips:@"请输入身份证号" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    
    
    
    AuthenView2ViewController *authenVC = [[AuthenView2ViewController alloc] init];
    authenVC.nameStr = self.nameTf.text;
    authenVC.phoneStr = self.phoneTf.text;
    authenVC.idNumStr = self.IDNumTf.text;
    [self.navigationController pushViewController:authenVC animated:YES];
}

-(void)createTopView
{
    self.av = [[AuthenView alloc] init];
    self.av.backgroundColor = [UIColor whiteColor];
    self.av.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
    [self.view addSubview:self.av];
}
-(void)createTableView
{
    self.titleArray = @[@"姓名",@"手机号",@"身份证号"].mutableCopy;
    self.titleValueArray = @[@"请输入真实姓名",@"请输入您的手机号",@"请输入您的身份证号码"].mutableCopy;
    
    for (int i = 0; i < self.titleValueArray.count; i++) {
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:self.titleValueArray[i]];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1]
                            range:NSMakeRange(0, [self.titleValueArray[i] length])];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:16]
                            range:NSMakeRange(0, [self.titleValueArray[i] length])];
        
        [self.attTitleValueArray addObject:placeholder];
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, ETScreenW,ETScreenH - 50 - 64) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = ETColorWithRGB(242, 244, 246);
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturedDetected:)];
    tapGesture.delegate = self;
    [self.tableView addGestureRecognizer:tapGesture];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[AuthenTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AuthenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArray[indexPath.row];
    cell.titleValueTf.attributedPlaceholder = self.attTitleValueArray[indexPath.row];
    cell.titleValueTf.delegate = self;
    cell.titleValueTf.returnKeyType = UIReturnKeyDone;
    //点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row != 0) {
        
//        cell.titleValueTf.keyboardType = UIKeyboardTypePhonePad;
    }
    //最后cell隐藏分割线
    if (indexPath.row == self.titleArray.count - 1) {
        cell.lineView.hidden = YES;
    }
    
    if (indexPath.row == 0) {
        self.nameTf = cell.titleValueTf;
    }
    
    if (indexPath.row == 1) {
        self.phoneTf = cell.titleValueTf;
        cell.titleValueTf.text = [AppTools shareInstance].userInfo.loginName;
        cell.titleValueTf.userInteractionEnabled = NO;
    }
    
    if (indexPath.row == 2) {
        self.IDNumTf = cell.titleValueTf;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AuthenTableViewCell *cell = (AuthenTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell.titleValueTf becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    
    return  NO;
}

-(void)tapGesturedDetected:(UIGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}

@end
